//
//  WYRedPacketMessageController.m
//  Boss
//
//  Created by admin on 2018/6/2.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYRedPacketMessageController.h"
#import "ZYMainController.h"
#import "WYBaseNavigationController.h"
#import "WYReportController.h"
#import "WYRedPacketMessageController+WYLogic.h"
#import "WYRedPacketMessageHeadCell.h"
#import "WYReceiveListCell.h"
#import "WYIntroductionCell.h"
#import "WYDepictsImageCell.h"
#import "UITableView+XHWebImageAutoSize.h"
#import "WYCommentCell.h"
#import "WYRedBottomView.h"
#import "CMInputView.h"
#import "WYCommentView.h"
#import "WYAdvertisementCell.h"
#import "WYReceiveDetailController.h"
#import "WYReportController.h"
#import "WYRedPacketMessage.h"
#import "WYSendLocationCell.h"
#import "WYCommentList.h"
#import "WYTaskController.h"
#import "ZFPlayerView.h"
#import "ZFBrightnessView.h"
#import "WYOutLinkController.h"
#import "WYShareBottomView.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
#import "WYVIPController.h"
#import "WYCommentController.h"
#import "WYPersonWebHomePageController.h"
#import "WYBusinessDetailController.h"
#define ZFPlayerShared                      [ZFBrightnessView sharedBrightnessView]
@interface WYRedPacketMessageController ()<UITableViewDelegate,UITableViewDataSource,WYIntroductionCellDelegate,WYDepictsImageCellDelegate,WYCommentViewDelegate,WYCommentCellDelegate,ZFPlayerDelegate,WYAdvertisementCellDelegate,WYRedPacketMessageHeadCellDelegate,KSPhotoBrowserDelegate>{
    BOOL _isReturn;
    BOOL _isAd;
}
@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)WYCommentView *commentView;

@property (nonatomic,assign)NSUInteger type;

@property (nonatomic,strong)NSMutableArray *commentArray;

@property(nonatomic ,strong) ZFPlayerView *playerView;
@property (nonatomic,strong)WYShareBottomView *shareBottomView;

@property (nonatomic,strong)NSString *attentionState;

@property (nonatomic,assign)BOOL refresh;
@property (nonatomic,assign)NSUInteger page;

@property (nonatomic,strong)WYRedBottomView *redBottomView;
@property (nonatomic,assign)BOOL isReplay;
@property (nonatomic,strong)WYCommentList *list;
@end

@implementation WYRedPacketMessageController

-(NSMutableArray *)commentArray
{
    if (!_commentArray) {
        _commentArray = [[NSMutableArray alloc]init];
    }
    return _commentArray;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //[self loadFollow];
}
-(void)loadFollow
{
    NSLog(@"%@",self.redMessage.grantUserId);
    
    WEAKSELF
    [WYHttpServer checkAttentionState:self.redMessage.grantUserId SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            NSString *attentionState = [response[@"result"][@"attentionState"] stringValue];
            self.attentionState = attentionState;
            [weakSelf.tableView reloadData];
        }else{
            
        }
        
        
        
    }];
    
    
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.playerView resetPlayer];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.redPacket.redpacketType isEqualToString:@"1"]||[self.redPacket.redpacketType isEqualToString:@"3"]) {
        _isAd = YES;
    }else{
        _isAd = NO;
    }
    [self setupUI];
    if (self.redPacket.isReceived.integerValue||self.isdymic||self.redPacket.isReceiveFinished.integerValue||self.isCountDown) {
        
    }else{
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        [self changeTime];
    }
    
    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification
                                               object:nil];
    // Do any additional setup after loading the view.
    [self loadMessage];
    self.view.backgroundColor = [UIColor whiteColor];
    
     self.page = 1;
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        weakSelf.page++;
        [weakSelf loadCommentList];
        // [weakSelf loadDataWithYear:self.year];
    }];
    
    
    
    
}

-(void)loadPacketUrl
{
    WEAKSELF
    [WYHttpServer shareToRedPacket:self.packetId WithType:self.isdymic?@"2": self.redPacket.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            NSString *linkUrl = response[@"result"];
            [weakSelf.shareBottomView setKLinkURL:linkUrl];
            if (weakSelf.redMessage) {
              [weakSelf.shareBottomView setKLinkDescription:weakSelf.redMessage.content];
            }else{
                if (weakSelf.advertisementMessage.comment.length) {
                    [weakSelf.shareBottomView setKLinkDescription:weakSelf.advertisementMessage.comment];
                }else{
                     [weakSelf.shareBottomView setKLinkDescription:weakSelf.advertisementMessage.title];
                }
            }
            weakSelf.shareBottomView.wait = NO;
            [weakSelf addForwardCount];
            switch (weakSelf.type) {
                case 0:{
                    [weakSelf.shareBottomView weChatShare:nil];
                }
                    break;
                case 1:{
                    [weakSelf.shareBottomView friendShatre:nil];
                }
                    break;
                case 2:{
                    [weakSelf.shareBottomView qqShare:nil];
                }
                    break;
                case 3:{
                    [weakSelf.shareBottomView QQZoneShare:nil];
                }
                    break;
                case 4:{
                    [weakSelf.shareBottomView weiBoShare:nil];
                }
                    break;
                default:
                    break;
            }
        }
    }];
    
    
    
    
}
-(void)addForwardCount
{
    [WYHttpServer addForwardCountWithRedpacketId:self.packetId WithRedpacketType:self.isdymic?@"2":self.redPacket.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            
        }
        
        
    }];
    
    
    
}

-(void)loadCommentList
{
    
    NSString *messageId =@"";
    if (self.isdymic) {
        messageId =self.messageId;
    }else{
        messageId =self.redMessage.messageId;
    }
    if (!messageId.length&&self.advertisementMessage) {
        messageId = self.advertisementMessage.redpacketId;
    }
     NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    WEAKSELF
    [WYHttpServer getRedPacketCommentList:messageId WithCurrentPage:page WithRedpacketType:self.isdymic?@"2":  self.redPacket.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
     
        if (success) {
            if (weakSelf.refresh) {
                [weakSelf.commentArray removeAllObjects];
            }
            NSMutableArray *array = [WYCommentList getCommentListFromJosn:response[@"result"][@"detail"]];
            if (array.count) {
                for (NSObject *objc in array) {
                    [weakSelf.commentArray addObject:objc];
                }
            }
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            weakSelf.redBottomView.commentLabel.text =[response[@"result"][@"totalCount"] stringValue];
            
            
        }else{
            
        }
        
        
        
    }];
    
    
    
}
-(void)loadMessage
{
    NSString *packetID =self.isdymic?self.packetId:self.redPacket.redpacketId;
    self.packetId = packetID;
    WEAKSELF
    [WYHttpServer getRedPacketInfoWithRedpacketId:packetID WithRedpacketType: self.isdymic?@"2":self.redPacket.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            
            if ([weakSelf.redPacket.redpacketType isEqualToString:@"1"]||[weakSelf.redPacket.redpacketType isEqualToString:@"3"]) {
                weakSelf.advertisementMessage = [WYAdvertisementMessage getAdvertisementMessageFronJson:response[@"result"]];
                [weakSelf.tableView reloadData];
                if (weakSelf.redPacket.isReceived.integerValue||weakSelf.isdymic||weakSelf.redPacket.isReceiveFinished.integerValue) {
                    if (weakSelf.advertisementMessage.pageView.intValue>1000) {
                         [weakSelf.leftBarButton setTitle:[NSString stringWithFormat:@"%.1f万人看过",self.advertisementMessage.pageView.floatValue/10000] forState:(UIControlStateNormal)];
                    }else{
                        [weakSelf.leftBarButton setTitle:[NSString stringWithFormat:@"%@人看过",self.advertisementMessage.pageView] forState:(UIControlStateNormal)];
                    }
                    
                    weakSelf.redBottomView.forwardLabel.text = weakSelf.advertisementMessage.transpondNum;
                    
                    
                    weakSelf.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:weakSelf.leftBarButton];
                }
            }else{
                weakSelf.redMessage = [WYRedPacketMessage getRedPacketMessageFromJson:response[@"result"]];
                [weakSelf loadFollow];
                weakSelf.redBottomView.forwardLabel.text = weakSelf.redMessage.transpondNum;
                [weakSelf.tableView reloadData];
                if (weakSelf.redPacket.isReceived.integerValue||weakSelf.isdymic||weakSelf.redPacket.isReceiveFinished.integerValue) {
                    if (weakSelf.redMessage.pageView.intValue>1000) {
                        [weakSelf.leftBarButton setTitle:[NSString stringWithFormat:@"%.1f万人人看过",self.redMessage.pageView.floatValue/10000] forState:(UIControlStateNormal)];
                    }else{
                       [weakSelf.leftBarButton setTitle:[NSString stringWithFormat:@"%@人看过",self.redMessage.pageView] forState:(UIControlStateNormal)];
                    }
                    
                    
                    weakSelf.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:weakSelf.leftBarButton];
                }
                
            }
                
                
                
                [weakSelf loadCommentList];
                
                
            
            
            
        }else{
            
            
        }
        
        
    }];
}




- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat height = keyboardRect.size.height;
    //self.comment
        [self.commentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-height);
        }];
}
- (void)keyboardWillHide:(NSNotification *)aNotification{
    [self.commentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(164);
    }];
}
-(void)setupUI
{
    [self wr_setNavBarShadowImageHidden:YES];
    self.navigationItem.title = @"红包信息";
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:@"5s后退出" forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"通用_返回白"] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more_icon"] style:(UIBarButtonItemStylePlain) target:self action:@selector(moreAction:)];
    
   
    self.leftBarButton = button;
    button.frame = CGRectMake(0, 0, 128, 44);
    button.titleLabel.font = kFontSystem(16);
    [button setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
    if (self.redPacket.isReceived.integerValue||self.isdymic||self.redPacket.isReceiveFinished.integerValue||self.isCountDown) {
        [button setTitle:@"" forState:UIControlStateNormal];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
         [button addTarget:self action:@selector(dismiss:) forControlEvents:(UIControlEventTouchUpInside)];
    }else{
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    }
    
   
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WYRedPacketMessageHeadCell" bundle:nil] forCellReuseIdentifier:@"WYRedPacketMessageHeadCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYReceiveListCell" bundle:nil] forCellReuseIdentifier:@"WYReceiveListCell"];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    
    
    [self.tableView registerClass:[WYIntroductionCell class] forCellReuseIdentifier:@"WYIntroductionCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYDepictsImageCell" bundle:nil] forCellReuseIdentifier:@"WYDepictsImageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYCommentCell" bundle:nil] forCellReuseIdentifier:@"WYCommentCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WYAdvertisementCell" bundle:nil] forCellReuseIdentifier:@"WYAdvertisementCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYSendLocationCell" bundle:nil] forCellReuseIdentifier:@"WYSendLocationCell"];
    
    CGFloat bottomHeight = 49;
    if (kDevice_Is_iPhoneX) {
        bottomHeight = 83;
    }
    UIView *bottomView = [[UIView alloc]init];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(bottomHeight);
    }];
    bottomView.backgroundColor = [UIColor whiteColor];
    WYRedBottomView *rebBtmView = [[NSBundle mainBundle]loadNibNamed:@"WYRedBottomView" owner:nil options:nil].firstObject;
    self.redBottomView = rebBtmView;
    [bottomView addSubview:rebBtmView];
    [rebBtmView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    
    [rebBtmView.contantBtn addTarget:self action:@selector(contactHim:) forControlEvents:UIControlEventTouchUpInside];
     [rebBtmView.forwardBtn addTarget:self action:@selector(forwardAction:) forControlEvents:UIControlEventTouchUpInside];
     [rebBtmView.commentBtn addTarget:self action:@selector(commentAction:) forControlEvents:UIControlEventTouchUpInside];
   
    
    self.commentView= [[WYCommentView alloc]init];
    self.commentView.comment.text = @"回复";
    self.commentView.delegate = self;
    [self.view addSubview:self.commentView];

    
    [self.commentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(164);
        make.bottom.mas_equalTo(164);
    }];
    [self.view addSubview:self.shareBottomView];
    WEAKSELF
    self.shareBottomView.complete = ^(NSUInteger type) {
        weakSelf.type = type;
        [weakSelf loadPacketUrl];
    };
}

-(void)dismiss:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
//    if (self.presentingViewController) {
//        [self dismissViewControllerAnimated:YES completion:nil];
//    } else {
//        [self.navigationController popViewControllerAnimated:YES];
//    }
    
}




- (void)contactHim:(id)sender {
    
    NSString *callPhone = @"";
    if (!self.advertisementMessage) {
        callPhone = [NSString stringWithFormat:@"telprompt://%@", self.redMessage.mobile];
    }else{
        callPhone = [NSString stringWithFormat:@"telprompt://%@", @"400-666-8055"];
    }
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
    }
}
- (void)forwardAction:(id)sender {
     self.shareBottomView.wait = YES;
     self.shareBottomView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
}
- (void)commentAction:(id)sender {
    self.isReplay = NO;
    self.commentView.comment.text = @"评论";
    self.commentView.textView.placeholder = @"禁止在评论区发广告";
    [self.commentView.textView becomeFirstResponder];
    NSLog(@"点击了评论 ");
}
-(void)clickSendButton
{
       WEAKSELF
    if (!self.commentView.textView.text.length) {
        [self toast:@"请输入评论内容"];
        [self.commentView.textView becomeFirstResponder];
        return;
    }
    
    if (!self.isReplay) {
        NSString *messageId =@"";
        if (self.isdymic) {
            messageId =self.messageId;
        }else{
            messageId =self.redMessage.messageId;
        }
        if (!messageId.length&&self.advertisementMessage) {
            messageId = self.advertisementMessage.redpacketId;
        }
     
        [WYHttpServer sendRedPacketCommentWithMessageID:messageId WithContent:self.commentView.textView.text WithRedpacketType:self.isdymic?@"2": self.redPacket.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
            if (success) {
                weakSelf.refresh = YES;
                [weakSelf loadCommentList];
            }else{
                [weakSelf toast:errorMessage];
            }
        }];
    }else{
        NSString *messageId =@"";
        if (self.isdymic) {
            messageId =self.messageId;
        }else{
            messageId =self.redMessage.messageId;
        }
        if (!messageId.length&&self.advertisementMessage) {
            messageId = self.advertisementMessage.redpacketId;
        }
        [WYHttpServer addRedPacketCommentReplayWithMessageId:messageId WithCommentId:self.list.commentId WithToReplayId:@"0" WithContent:self.commentView.textView.text WithToUserId:self.list.userId WithToUserName:self.list.userName WithRedPacketType:self.redPacket.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
            if (success) {
                weakSelf.refresh = YES;
                [weakSelf loadCommentList];
            }else{
                [weakSelf toast:errorMessage];
            }
        }];
    }
    
    
    
    
    
    
}
-(void)moreAction:(UIBarButtonItem *)item
{
    [self showAlertAction];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        if (_isAd) {
            if ([self.advertisementMessage.type isEqualToString:@"3"]) {
                return 3;
            }
            return 2;
        }else{
           return 3;
        }
    }else if (section==1) {
        if (_isAd) {
            if ([self.advertisementMessage.type isEqualToString:@"3"]) {
                return self.advertisementMessage.images.count;
            }
            return 1;
        }else{
            return self.redMessage.images.count;
        }
    }else if (section==2){
        if (_isAd) {
            return 0;
        }
        return 1;
    }else{
        return self.commentArray.count;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0&&indexPath.row==0) {
        WYRedPacketMessageHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYRedPacketMessageHeadCell" forIndexPath:indexPath];
        
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"WYRedPacketMessageHeadCell" owner:nil options:nil].firstObject;
        }
        cell.delegate = self;
        if (_isAd) {
            if (self.advertisementMessage) {
                [cell setAdvertisementMessage:self.advertisementMessage];
            }
        }else{
            if (self.redMessage) {
                [cell setRedMessage:self.redMessage];
            }

        }
        if (self.attentionState) {
            [cell setAttentionState:self.attentionState];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row==1&&indexPath.section==0) {
        WYReceiveListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYReceiveListCell" forIndexPath:indexPath];
        
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"WYReceiveListCell" owner:nil options:nil].firstObject;
        }
        [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
        if (_isAd) {
            if (self.advertisementMessage) {
                cell.dataArray = self.advertisementMessage.receiveUserList;
            }
        }else{
            if (self.redMessage) {
                cell.dataArray = self.redMessage.receiveUserList;
            }
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.row==2&&indexPath.section==0) {
        WYIntroductionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYIntroductionCell" forIndexPath:indexPath];
        
        if (!cell) {
            cell = [[WYIntroductionCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"WYIntroductionCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        [cell setType:@""];
    }
    
    
    if (indexPath.section==1) {
        
        if (_isAd&&![self.advertisementMessage.type isEqualToString:@"3"]) {
            WYAdvertisementCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WYAdvertisementCell" forIndexPath:indexPath];
            
             [cell setAdvertisementMessage:self.advertisementMessage];
            cell.delegate = self;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
           // [self clickPlayButtonWithCell:cell];
            
            return cell;
            
        }else{
            WYDepictsImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYDepictsImageCell" forIndexPath:indexPath];
            
            if (!cell) {
                cell = [[NSBundle mainBundle]loadNibNamed:@"WYDepictsImageCell" owner:nil options:nil].firstObject;
            }
            cell.delegate = self;
            if (self.redMessage) {
               [cell setImageUrl:self.redMessage.images[indexPath.row]];
            }
            if (self.advertisementMessage) {
                [cell setImageUrl:self.advertisementMessage.images[indexPath.row]];
                if (indexPath.row == self.advertisementMessage.images.count-1) {
                    cell.bottomView.hidden = NO;
                    cell.bottomViewHeight.constant = 34;
                    cell.companyLabel.text = self.advertisementMessage.title;
                    cell.countLabel.text = [NSString stringWithFormat:@"%@阅读量",self.advertisementMessage.pageView];
                }
                
                
            }
            
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
         //   cell.backgroundColor = [UIColor redColor];
            return cell;
        }
        
        
      
    }
    
    if (indexPath.section==2) {

        WYSendLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYSendLocationCell" forIndexPath:indexPath];
        if (self.redMessage) {
            if (self.redMessage.address.length) {
                cell.locationLabel.text = self.redMessage.address;
            }else{
                cell.locationLabel.text = @"该用户隐藏了位置信息";
            }
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }
    
    
    if (indexPath.section==3) {
        WYCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYCommentCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"WYCommentCell" owner:nil options:nil].firstObject;
        }
        [cell setList:self.commentArray[indexPath.row]];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    WYIntroductionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYIntroductionCell" forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[WYIntroductionCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"WYIntroductionCell"];
    }
    cell.delegate = self;
    if (self.redMessage) {
        [cell setOutLink:self.redMessage.outLink];
        [cell setType:self.redMessage.content];
    }
    if ([self.advertisementMessage.type isEqualToString:@"3"]) {
        [cell setOutLink:self.advertisementMessage.web];
        [cell setType:self.advertisementMessage.comment];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==3) {
        if (self.commentArray.count) {
            return 35;
        }
       
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==3) {
        if (self.commentArray.count) {
            UIView *view = [[UIView alloc]init];
            UIView *lineView = [[UIView alloc]init];
            lineView.backgroundColor = [UIColor hexStringToColor:@"#f7f7f7" alpha:1.0];
            [view addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.mas_equalTo(0);
                make.height.mas_equalTo(10);
            }];
            
            UILabel *label = [[UILabel alloc]init];
            [view addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(16);
                make.top.equalTo(lineView.mas_bottom).offset(10);
            }];
            label.text = @"这些BOSS都发表了观点";
            label.textColor = [UIColor hexStringToColor:@"#999999" alpha:1.0];
            label.font = [UIFont systemFontOfSize:12];
            view.backgroundColor = [UIColor whiteColor];
            return view;
        }
    }
    return nil;
}

#pragma mark WYCommentCellDelegate
-(void)clickDeleteButtonWithCell:(UITableViewCell *)cell
{
    WYCommentCell *commentCell = (WYCommentCell *)cell;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:commentCell];
    WYCommentList *list = self.commentArray[indexPath.row];
    
    
    [self.commentArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
    [WYHttpServer DeleteRedPacketCommentWithCommentId:list.commentId WithRedpacketType:self.redPacket.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            
        }
    }];
 
}


#pragma mark - WYAdvertisementCellDelegate
-(void)clickPlayButtonWithCell:(WYAdvertisementCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZFPlayerModel *playerModel = [[ZFPlayerModel alloc] init];
    playerModel.title            = self.advertisementMessage.title;
    playerModel.videoURL         =[NSURL URLWithString:self.advertisementMessage.videoUrl] ;
    playerModel.placeholderImageURLString = self.advertisementMessage.videoPic;
    playerModel.scrollView       = self.tableView;
    playerModel.indexPath        = indexPath;
    // 赋值分辨率字典
    //  playerModel.resolutionDic    = div;
    // player的父视图tag
    playerModel.fatherViewTag    = cell.adImageView.tag;
    
    // 设置播放控制层和model
    [self.playerView playerControlView:nil playerModel:playerModel];
    // 下载功能
    self.playerView.hasDownload = NO;
    // 自动播放
    [self.playerView autoPlayTheVideo];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0&&indexPath.row==1) {
        WYReceiveDetailController *rdc = [[WYReceiveDetailController alloc]init];
        if (_isAd) {
              rdc.advertisementMessage = self.advertisementMessage;
        }else{
            rdc.redMessage =self.redMessage;
        }
        
        
        rdc.redPacket = self.redPacket;
        [self.navigationController pushViewController:rdc animated:YES];
    }
    WEAKSELF
    if (indexPath.section==1) {
        if (_isAd) {
           
        }else{
            
            NSLog(@"1231312313131313123131231312312");
            
            NSMutableArray *items = @[].mutableCopy;
            for (int i = 0; i < self.redMessage.images.count; i++) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:1];
                WYDepictsImageCell *cell =( WYDepictsImageCell *)[tableView cellForRowAtIndexPath:indexPath];
                UIImageView *imgV = cell.depictsImageView;
               // UIImage *image =cell.depictsImageView.image;
                KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",self.redMessage.images[i]]]];
                [items addObject:item];
            }
            KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
            browser.delegate = self;
            browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleSlide;
            browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
            browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
            browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
            browser.bounces = NO;
            [browser showFromViewController:self];
        }
    }
    if (indexPath.section==3) {
        self.isReplay = YES;
        WYCommentList *list = self.commentArray[indexPath.row];
        self.list = list;
        WYUserMessage *userMessage = [WYStoreModule currentUserMessage];
        if ([userMessage.id isEqualToString:list.userId]) {
            return;
        }
        
        
        [self.commentView.textView becomeFirstResponder];
        self.commentView.comment.text = @"回复";
        self.commentView.textView.placeholder = [NSString stringWithFormat:@"回复 %@",list.userName];
        
    }
    
    
    
    
    
}
-(void)gotoVip
{
    WYVIPController *vpc = [[WYVIPController alloc]init];
    [self.navigationController pushViewController:vpc animated:YES];
}
-(void)didTaskButton
{
    WYTaskController *tvc = [[WYTaskController alloc]init];
    [self.navigationController pushViewController:tvc animated:YES];
}

#pragma  mark  WYIntroductionCellDelegate
-(void)cell:(WYIntroductionCell *)cell didClickInLabel:(YYLabel *)label textRange:(NSRange)textRange WithOutLink:(NSString *)outLink
{
    WYOutLinkController *pvc = [[WYOutLinkController alloc]init];
    pvc.urlString = outLink;
    [self.navigationController pushViewController:pvc animated:YES];

    
    NSLog(@"点击了 网页链接");
}
#pragma mark WYDepictsImageCellDelegate
-(void)reloadTableView:(NSString *)cell
{
    [self.tableView xh_reloadDataForURL:[NSURL URLWithString:cell]];
}
-(void)clickFollowActionWithType:(NSString *)type
{
    BOOL state = type.intValue;
    state = !state;
    
    
    WEAKSELF
    [WYHttpServer followUserOrUnFollowUser:self.redMessage.grantUserId WithAttentionState:[NSString stringWithFormat:@"%d",state] SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
//            _isRefresh = YES;
            [weakSelf loadFollow];
        }else{
            
        }
        
        
        
    }];
    
    
}

-(void)clickImageViewWithUrl:(NSString *)urlString WithLinkType:(NSString *)linkType WithLinkName:(NSString *)name
{
    if ([linkType isEqualToString:@"1"]) {
        WYBusinessDetailController *bcv = [[WYBusinessDetailController alloc]init];
        bcv.navTitle = name;
        bcv.bussinessCode = urlString;
        [self.navigationController pushViewController:bcv animated:YES];
    }else{
        WYOutLinkController *ovc = [[WYOutLinkController alloc]init];
        ovc.urlString = urlString;
        [self.navigationController pushViewController:ovc animated:YES];
    }
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight-44) style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight-self.tabHeight);
    }
    return _tableView;
}
-(ZFPlayerView *)playerView{
    if (!_playerView) {
        _playerView = [ZFPlayerView sharedPlayerView];
        _playerView.delegate = self;
        // 当cell 播放视频由全屏变为小屏的时候，不回到中间的位置
        _playerView.cellPlayerOnCenter = NO;
        _playerView.forcePortrait = NO;
        ZFPlayerShared.isLockScreen = YES;
        ZFPlayerShared.isStatusBarHidden = NO;
        
    }
    return _playerView;
}
-(WYShareBottomView *)shareBottomView
{
    if (!_shareBottomView) {
        _shareBottomView = [WYShareBottomView shareedShareBottomView];
        _shareBottomView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight);
        [_shareBottomView setBottomHeightStr:@""];
        [_shareBottomView setKLinkTitle:@"来自巨牛BOSS的红包信息"];
        [_shareBottomView setKLinkDescription:@"点击即可领取该信息中的红包哦！"];
        [_shareBottomView setShareImage:[UIImage imageNamed:@"company_lolo"]];
    }
    return _shareBottomView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    if (offset.y <= 0) {
        offset.y = 0;
    }
    scrollView.contentOffset = offset;

    CGFloat sectionHeaderHeight = 35;
    
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
}
-(void)seeMoreComment:(WYCommentList *)list
{
    WYCommentController *cvc = [[WYCommentController alloc]init];
    cvc.redpacketType = self.redPacket.redpacketType;
    cvc.commentId = list.commentId;
    
    [self.navigationController pushViewController:cvc animated:YES];
}
-(void)clickImageViewWithUserId:(NSString *)userId
{
    WYPersonWebHomePageController *web = [[WYPersonWebHomePageController alloc]init];
    web.urlString = userId;
    [self.navigationController pushViewController:web animated:YES];
}
-(void)gotoPageWithUserId:(NSString *)userId
{
    WYPersonWebHomePageController *web = [[WYPersonWebHomePageController alloc]init];
    
    web.urlString = userId;
    [self.navigationController pushViewController:web animated:YES];
}
@end
